package com.woniu.mall.service.impl;


import com.woniu.mall.constant.BusinessRedisKey;
import com.woniu.mall.constant.PlatformRedisKey;
import com.woniu.mall.pojo.Coupon;
import com.woniu.mall.service.CouponBusinessService;
import com.woniu.mall.service.CouponPlatformService;
import com.woniu.mall.service.CouponUserService;
import com.woniu.user.feignClient.UserFeignClient;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 1. @description:
 * 2. @author: Lingger
 * 3. @time: 2023/5/23
 */
@Service
public class CouponCouponUserServiceImpl implements CouponUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CouponPlatformService couponPlatformService;

    @Autowired
    private CouponBusinessService couponBusinessService;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public Boolean buyRedEnvelope(String mobile) {

        //通过手机号 使用openfeign查询到User信息
        UserLoginVO user = userFeignClient.getByMobile(mobile).getData();
        //判断该手机号是否重复购买
        Object redEnvelope = redisTemplate.boundValueOps(PlatformRedisKey.RED_ENVELOPE + mobile).get();
        if (redEnvelope != null) {
            return false;
        }
        //判断用户的比特币是否足够 （比特币>=15） 如果不够 返回false
        if (user.getBitcoin().compareTo(PlatformRedisKey.RED_ENVELOPE_PRICE) < 0) {
            return false;
        }
        //如果足够，调用用户服务中的扣减比特币接口
        userFeignClient.updateAmount(mobile, PlatformRedisKey.RED_ENVELOPE_PRICE);

        //生成红包 设置过期时间
        Coupon coupon = setExpirationTime(couponPlatformService.saveRedEnvelope());
        //保存到redis中,并设置过期时间
        redisTemplate.boundValueOps(PlatformRedisKey.RED_ENVELOPE + mobile).set(coupon, 120, TimeUnit.DAYS);
        return true;
    }

    @Override
    public Boolean buyBusinessCoupon(Integer storeId, UserLoginVO userLoginVO) {

        //判断商户是否发布优惠券
        Object businessCoupon = redisTemplate.boundHashOps(BusinessRedisKey.BUSINESS_COUPON).get(storeId.toString());
        if (businessCoupon == null) {
            return false;
        }

        //判断是否重复购买
        Object bussinessCoupon = redisTemplate.boundHashOps(BusinessRedisKey.BUSINESS_COUPON + storeId).get(userLoginVO.getId().toString());
        if (bussinessCoupon != null){
            return false;
        }

        //通过手机号 使用openfeign查询到User信息
        UserLoginVO user = userFeignClient.getByMobile(userLoginVO.getMobile()).getData();

        //不允许购买自己商店的优惠券
        if (user.getStoreId() == storeId) {
            return false;
        }

        //判断用户的比特币是否足够 （比特币>=4） 如果不够 返回false
        if (user.getBitcoin().compareTo(BusinessRedisKey.BUSINESS_COUPON_PRICE) < 0) {
            return false;
        }

        //如果足够，调用用户服务中的扣减比特币接口
        userFeignClient.updateAmount(userLoginVO.getMobile(), BusinessRedisKey.AMOUNT_4);

        //保存到redis中
        Coupon coupon = couponBusinessService.createBusinessCoupon();
        redisTemplate.boundHashOps(BusinessRedisKey.BUSINESS_COUPON + storeId).put(userLoginVO.getMobile(), coupon);
        return true;
    }

    //设置过期时间
    private Coupon setExpirationTime(Coupon coupon) {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MONTH, 1);
        Date expirationTime = calendar.getTime();

        coupon.setExpirationTime(expirationTime);
        return coupon;
    }
}
